package com.temple.common.omponents;

import com.google.common.collect.Lists;
import org.dozer.DozerBeanMapper;
import org.dozer.Mapper;
import org.dozer.util.MappingValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;


@Component
public class DozerHolder {

    private Mapper dozerMapper = new DozerBeanMapper();

    public DozerHolder() {
    }

    /**
     * 集合转换
     */
    public <T> List<T> convert(List source, Class<T> destinationClass) {
        if (CollectionUtils.isEmpty(source)) {
            return Lists.newArrayList();
        }
        MappingValidator.validateMappingRequest(source, destinationClass);
        List target = new ArrayList(source.size());
        for (Object each : source) {
            target.add(dozerMapper.map(each, destinationClass));
        }
        return target;
    }

    /**
     *  @Description :转换单个对象
     */
    public <T> T convert(Object source, Class<T> destinationClass) {
        if(source == null){
            return null;
        }
        MappingValidator.validateMappingRequest(source, destinationClass);
        return dozerMapper.map(source, destinationClass);
    }


}
